package com.clqteam.daoimpl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.clqteam.dao.showZsDao;
import com.clqteam.model.ZsProduct;
import com.clqteam.util.JDBCutil;

public class showZsDaoImpl implements showZsDao{

	@Override
	public int getZsProductCount() {
		int count = 0;
		StringBuffer sql = new StringBuffer();
		sql.append(" SELECT COUNT(s_zs.zsId) AS pcount");
		sql.append(" FROM s_zs");
		Connection con = JDBCutil.getConnection();
		try {
			PreparedStatement pst = con.prepareStatement(sql.toString());
			ResultSet rst = pst.executeQuery();
			if (rst.next()) {
				count = rst.getInt("pcount");
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return count;
	}

	@Override
	public List<ZsProduct> getAllZsProduct(int page, int rows) {
		List<ZsProduct> pros = new ArrayList<>();
		StringBuffer sql = new StringBuffer();
		sql.append(" SELECT * FROM s_zs");
		sql.append(" ORDER BY s_zs.zspId");
		sql.append(" LIMIT ?,?");
		Connection con = JDBCutil.getConnection();
		try {
			PreparedStatement pst = con.prepareStatement(sql.toString());
			pst.setInt(1,(page-1)*rows);
			pst.setInt(2, rows);
			ResultSet rst = pst.executeQuery();
			while (rst.next()) {
				ZsProduct p = new ZsProduct();
				p.setZspId(rst.getInt("zspId"));
				p.setZspName(rst.getString("zspName"));
				p.setZspAmount(rst.getInt("zspAmount"));
				p.setZspCost(rst.getDouble("zspCost"));
				p.setZspTime(rst.getString("zspTime"));
				p.setZspPeople(rst.getString("zspPeople"));
				p.setZsReason(rst.getString("zsReason"));
				p.setZsId(rst.getInt("zsId"));
				pros.add(p);
				
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return pros;
	}

}
